package cn.lingyangwl.agile.infra.gen.manager;

import cn.lingyangwl.agile.infra.gen.mapper.TemplateConfigMapper;
import cn.lingyangwl.agile.infra.gen.model.entity.TemplateConfig;
import cn.lingyangwl.agile.infra.gen.model.rqrs.CopyTemplateReq;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Objects;

/**
 * @author shenguangyang
 */
@Repository
public class TemplateConfigManager extends ServiceImpl<TemplateConfigMapper, TemplateConfig>
        implements IService<TemplateConfig> {

    public TemplateConfig getTemplateByName(CopyTemplateReq req) {
        LambdaQueryWrapper<TemplateConfig> queryWrapper = TemplateConfig.lqw()
                .eq(Objects.nonNull(req.getSourceGroupId()), TemplateConfig::getGroupId, req.getSourceGroupId())
                .eq(StringUtils.isNotEmpty(req.getName()), TemplateConfig::getName, req.getName());
        return this.getOne(queryWrapper);
    }

    public TemplateConfig getTemplateByNameInGroup(String name, Long groupId) {
        LambdaQueryWrapper<TemplateConfig> queryWrapper = TemplateConfig.lqw()
                .eq(Objects.nonNull(groupId), TemplateConfig::getGroupId, groupId)
                .eq(StringUtils.isNotEmpty(name), TemplateConfig::getName, name);
        return this.getOne(queryWrapper);
    }

    public void updateByNameAndGroup(TemplateConfig req) {
        LambdaQueryWrapper<TemplateConfig> queryWrapper = TemplateConfig.lqw()
                .eq(Objects.nonNull(req.getGroupId()), TemplateConfig::getGroupId, req.getGroupId())
                .eq(StringUtils.isNotEmpty(req.getName()), TemplateConfig::getName, req.getName());
        this.update(req, queryWrapper);
    }
}
